package com.complex.editejb.dao;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: AGrankin
 * Date: 29.08.13
 * Time: 13:52
 * To change this template use File | Settings | File Templates.
 */

@Stateless(name = "EditDAOBeanName", mappedName = "EditDAOBeanName")
@Remote(IEditDAOBean.class)
public class EditDAOBean implements IEditDAOBean {

    private QueueSession queueSession = null;
    private QueueSender queueSender = null;
    private TextMessage myMessage = null;

    public EditDAOBean() {

        try {

            Context context = new InitialContext();
            ConnectionFactory factory = (ConnectionFactory)context.lookup("javax.jms.QueueConnectionFactory");
            Queue queue = (Queue)context.lookup("testQueue");

            QueueConnection queueConnection = (QueueConnection)factory.createConnection();
            queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            queueSender = queueSession.createSender(queue);

        } catch (NamingException e) {
            e.printStackTrace();
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }

    }

    @Override
    public void insertNode(int parentID, String nodeName) {
        try {
            myMessage = queueSession.createTextMessage();
            myMessage.setText("INSERT INTO nodes (Parent_ID,N_Name) VALUES (" + parentID + ",'" + nodeName + "')");
            queueSender.send(myMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }
    }

    @Override
    public void deleteNode(List<String> attrsIDList, List<String> nodesIDList) {
        try {
            myMessage = queueSession.createTextMessage();
            for(int i = 0; i < attrsIDList.size(); i++) {
                myMessage.setText("DELETE FROM attributes WHERE N_ID=" + attrsIDList.get(i));
                queueSender.send(myMessage);
            }
            for(int i = 0; i < nodesIDList.size(); i++){
                myMessage.setText("DELETE FROM nodes WHERE N_ID=" + nodesIDList.get(i));
                queueSender.send(myMessage);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }
    }

    @Override
    public void editNode(int nodeID, String newNodeName) {
        try {
            myMessage = queueSession.createTextMessage();
            myMessage.setText("UPDATE nodes SET N_Name='" + newNodeName + "' WHERE N_ID='" + nodeID + "'");
            queueSender.send(myMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }
    }

    @Override
    public void changeParent(int nodeID, int newParentID) {
        try {
            myMessage = queueSession.createTextMessage();
            myMessage.setText("UPDATE nodes SET Parent_ID='" + newParentID + "' WHERE N_ID='" + nodeID + "'");
            queueSender.send(myMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }
    }

    @Override
    public void insertAttribute(int nodeID, String attrName, String attrValue) {
        try {
            myMessage = queueSession.createTextMessage();
            myMessage.setText("INSERT INTO attributes (N_ID,A_Name,A_Value) VALUES (" + nodeID + ",'" + attrName + "','" + attrValue + "')");
            queueSender.send(myMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }
    }

    @Override
    public void deleteAttribute(int attrID) {
        try {
            myMessage = queueSession.createTextMessage();
            myMessage.setText("DELETE FROM attributes WHERE A_ID=" + attrID);
            queueSender.send(myMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }
    }

    @Override
    public void editAttribute(int attrID, String newAttrName, String newAttrValue) {
        try {
            myMessage = queueSession.createTextMessage();
            myMessage.setText("UPDATE attributes SET A_Name='" + newAttrName + "',A_Value='" + newAttrValue + "' WHERE A_ID=" + attrID);
            queueSender.send(myMessage);
        } catch (JMSException e) {
            e.printStackTrace();
        } finally {
            myMessage = null;
        }
    }
}
